<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div>
        <h2 class="all">全部商品分类</h2>
          <div class="sort">
            <div class="all-sort-list2">
              <div v-for="(c1,index) in category"  :key="c1.id" class="item" :class="{cur:currentIndex==index}">
                <h3 @mouseenter="changeIndex(index)">
                  <a v-show="c1.type===1">{{c1.name}}</a>
                </h3>
                <div class="item-list clearfix" :style="{display:currentIndex==index?'block':'none'}">
                  <div class="subitem" v-for="c2 in category"  :key="c2.id">
                    <dl class="fore">
                      <dt v-show="c2.type===2">
                        <a v-if="c2.parentId===c1.id">{{c2.name}}</a>
                      </dt>
                      <dd>
                        <em v-for="c3 in category"  :key="c3.id">
                          <a  v-if="c3.type===3">
                          <a v-show="c3.parentId===c2.id">{{c3.name}}</a>
                          </a>
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
      </div>
      <nav class="nav">
        <a href="###">海鲜生鲜</a>
        <a href="###">水果蔬菜</a>
        <a href="###">夏日饮品</a>
        <a href="###">咖喱米饭</a>
        <a href="###">家用电器</a>
        <a href="###">手机专店</a>
        <a href="###">特价秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>

import {reqCategoryFindAll} from "@/api";
import {onMounted, ref, getCurrentInstance} from "vue";
import {ElMessage} from "element-plus";
import throttle from 'lodash/throttle'
export default {
name: "TypeNav",
  setup(){
  const category=ref([])
    const  currentIndex=ref(-1)
    const  show=ref(true)
    const {proxy}=getCurrentInstance()
  const data=async ()=>{
    let result=await reqCategoryFindAll()
    if (result.status===10000){
      proxy.category=result.data
    }else {
      ElMessage.success('系统异常')
    }
  }
    /**
     * 节流处理
     */
  const changeIndex=throttle((index)=>{
          proxy.currentIndex=index
  },50)
  onMounted(()=>{
    data()
  })
      return{category,
        currentIndex,
        show,
        changeIndex
  }
  }
}
</script>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #ff6700;
  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #ff6700;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .cur{
          background: #ff6700;
        }
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }
          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>

